package com.hg.oa.module.login.layer;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;

import com.hg.oa.R;
import com.hg.oa.common.BaseActivityWithToolBar;
import com.hg.oa.module.MainActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by fengxiang on 2016/8/16.
 */
public class LoginActivity extends BaseActivityWithToolBar {

    String tag = "LoginActivity";
    Unbinder unbinder;

    @BindView(R.id.btn_login)
    Button btn_login;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        unbinder = ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_login)
    void btn_login() {
        Observable.just(null)
                .subscribeOn(AndroidSchedulers.mainThread())
                .map(new Func1<Object, Object>() {
                    @Override
                    public Object call(Object o) {
                        showProgressDialog();
                        return null;
                    }
                })
                .subscribeOn(Schedulers.io())
                .delay(1500, TimeUnit.MILLISECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        hideProgressDialog();
                        startMain();
                    }
                });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


    ProgressDialog progressDialog;

    void showProgressDialog() {
        progressDialog = ProgressDialog.show(LoginActivity.this, "", "loading...");
    }

    void hideProgressDialog() {
        progressDialog.dismiss();
    }

    void startMain() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
